<?php
class Blog_model extends Model
{
	
	function __construct()
	{
		parent::factory();
	}
	
	
	function add($data)
	{
		$title = parent::$db->escape($data['title']);
		$body = parent::$db->escape($data['body']);
		$rss = (int) $data['rss'];
		$time = time();
		
		parent::$db->query("INSERT INTO t_blog(title, body, rss, date) VALUES('$title', '$body', $rss, $time)");
		
		return (int) parent::$db->getOne("SELECT LAST_INSERT_ID()");
		return;
	}
	
	function edit($data)
	{
		$title = parent::$db->escape($data['title']);
		$body = parent::$db->escape($data['body']);
		$rss = (int) $data['rss'];
		$id = (int) $data['id'];
		
		parent::$db->query("UPDATE t_blog SET title = '$title', body = '$body', rss = $rss WHERE id = $id");
	}
	
	function delete($ids)
	{
		if (is_array($ids))
			$ids = implode(',', $ids);
		else
			$ids = (int) $ids;
		
		
		parent::$db->query("DELETE FROM t_blog WHERE id IN ($ids)");
		
		return;
	}
	
	function getItems($rss = 0, $limit = 0)
	{
		return parent::$db->getAll("SELECT * FROM t_blog ".($rss ? 'WHERE rss = 1' : '')." ORDER BY id DESC ".($limit ? 'LIMIT 0,'.$limit : '')."");
	}
	
	
	function getItem($id)
	{
		return parent::$db->getRow("SELECT * FROM t_blog WHERE id = $id");
	}
	
	
	function __destruct(){}
}
?>